package com.basic.util;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.widget.Toast;

public class IntentTool {
	
	/**
	 * 新栈中启动activity 
	 * @param context
	 * @param clazz
	 */
	public static void callNewActivity(Context context,Class<? extends Activity> clazz){
		Intent intent = new Intent();
		intent.setClass(context, clazz);
		intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		startActivity(context, intent);
    }
	
    public static void callActivity(Context context,Class<? extends Activity> clazz){
		Intent intent = new Intent();
		intent.setClass(context, clazz);
		startActivity(context, intent);
    }
    
    public static void callActivityForResult(Context context,Class<? extends Activity> clazz,int requestCode)
    {
    	Intent intent = new Intent();
		intent.setClass(context, clazz);
		((Activity)context).startActivityForResult(intent, requestCode);
    }
    
    public static void callActivity(Context context, Class<? extends Activity> clazz, Bundle bunble){
		Intent intent = new Intent();
		intent.setClass(context, clazz);
		intent.putExtras(bunble);
		startActivity(context, intent);
    }
    
    
    public static boolean startActivity(Context context, Intent intent) {
    	if(isIntentResoled(context ,intent)) {
    		context.startActivity(intent);
    		return true;
    	} else {
    		return false;
    	}
    }
    
    public static boolean isIntentResoled(Context context, Intent intent) {
    	List<ResolveInfo> infos = context.getPackageManager().queryIntentActivities(intent, 0);
    	return !infos.isEmpty();
    }
    
    /**
     * 启动文本等形式关联程序
     * @param context
     * @param subject
     * @param text
     */
    public static void callShareActivity(Context context, String subject, String text) {
		Intent intent = new Intent(Intent.ACTION_SEND);
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_SUBJECT, subject);
		intent.putExtra(Intent.EXTRA_TEXT, text);
		context.startActivity(Intent.createChooser(intent, subject)); 
    }
    
    /**
     *	创建桌面快捷方式 
     * @param context
     * @param action
     * @param name
     * @param iconResId
     */
	public static void createShortcut(Context context, Intent action, String name, int iconResId) {
		Intent intent = new Intent("com.android.launcher.action.INSTALL_SHORTCUT");
		// 不会重复创建
		intent.putExtra("duplicate", false);
		intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, 
				Intent.ShortcutIconResource.fromContext(context, iconResId));
		
		action.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, action);
		intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, name);
		context.sendBroadcast(intent);
	}
	
	/**
	 * @return 导向桌面的intent
	 */
	public static Intent getHomeIntent() {
		Intent intent = new Intent(Intent.ACTION_MAIN);
		intent.addCategory(Intent.CATEGORY_HOME);
		return intent;
	}
}
